/***************************************************************************
*   Copyright (C) 2009 by Steven Waslander   *
*   stevenw@uwaterloo.ca   *
*                                                                         *
***************************************************************************/

#ifndef _STATS_H
#define _STATS_H

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <sys/time.h> // (required for seedRand)

inline void seedRand(void);
template <class T> inline T uniformRand(T lower = T(0), T upper = T(1));
template <class T> inline T normalRand(T mean = T(0), T stdev = T(1));

inline void seedRand(void)
{
  timeval tv; gettimeofday(&tv,NULL);
  unsigned int n = int(tv.tv_sec*1000000 + tv.tv_usec); std::srand(n);
}

template <class T> 
inline T uniformRand(T lower = T(0), T upper = T(1))
{
  const double norm = 1.0/(RAND_MAX + 1.0);
  return (lower + (T)((double(upper-lower)*std::rand())*norm));
}

template <class T> 
inline T normalRand(T mean = T(0), T stdev = T(1))
{
  const double norm = 1.0/(RAND_MAX + 1.0);
  return T(mean + stdev*(sqrt(-2.0*log(std::rand()*norm))*sin(2.0*M_PI*(std::rand()*norm))));
}

#endif // _STATS_H
